/**
 *Class AddWordScreen
 *
 * version 1.0
 *
 * @author Jin Kit Chua
 * @version 20130319 V1.0
 */
package ch.zbw.iCoach.kit;

//All the imports for this class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.zbw.iCoach.remo.MainLogic;

//start of the class
public class AddWordScreen {

	private Languages la;
	private MainLogic ml;

	private JFrame mainFrame;

	private JButton ok;
	private JButton cancel;

	private String selectedUser;

	private JTextField word1;
	private JTextField word2;
	private JTextField word3;
	private JTextField word4;

	private JLabel word1DE;
	private JLabel word2EN;
	private JLabel word3FR;
	private JLabel word4IT;

	private JPanel buttonPanel;
	private JPanel wordPanel;

	/**
	 * 
	 * @param ml  reference of the mainlogic so it can use the methods interface
	 * @param la reference of the languages for translation
	 * @param su a string for the selected user to add the word to this user
	 */
	public AddWordScreen(MainLogic ml, Languages la, String su) {

		this.selectedUser = su;
		this.ml = ml;
		this.la = la;
		this.mainFrame = new JFrame();

		this.ok = new JButton();
		this.cancel = new JButton();

		this.buttonPanel = new JPanel();
		this.wordPanel = new JPanel();

		this.word1DE = new JLabel();
		this.word2EN = new JLabel();
		this.word3FR = new JLabel();
		this.word4IT = new JLabel();

		this.word1 = new JTextField();
		this.word2 = new JTextField();
		this.word3 = new JTextField();
		this.word4 = new JTextField();

		paint();
	}

	public void paint() {

		// Initialise frame and GUI elements
		this.mainFrame.setBackground(Color.lightGray);
		this.mainFrame.setResizable(false);

		String tempLang = this.la.switchLangAddWord();// creating the languages
		String[] parts = tempLang.split(",");
		String wrd1 = parts[0];
		String wrd2 = parts[1];
		String wrd3 = parts[2];
		String wrd4 = parts[3];
		String buttonConfirm = parts[4];
		String buttonCancel = parts[5];
		String title = parts[6];

		this.word1DE.setText(wrd1);
		this.word2EN.setText(wrd2);
		this.word3FR.setText(wrd3);
		this.word4IT.setText(wrd4);
		this.ok.setText(buttonConfirm);
		this.cancel.setText(buttonCancel);
		this.mainFrame.setTitle(title);

		this.mainFrame.setLayout(new BorderLayout(10, 20));
		this.buttonPanel.setLayout(new GridLayout(1, 4, 20, 20));
		this.wordPanel.setLayout(new GridLayout(4, 2, 20, 20));

		this.buttonPanel.add(this.ok);
		this.buttonPanel.add(this.cancel);

		this.wordPanel.add(this.word1DE);
		this.wordPanel.add(this.word1);
		this.wordPanel.add(this.word2EN);
		this.wordPanel.add(this.word2);
		this.wordPanel.add(this.word3FR);
		this.wordPanel.add(this.word3);
		this.wordPanel.add(this.word4IT);
		this.wordPanel.add(this.word4);

		this.mainFrame.add(this.wordPanel, BorderLayout.CENTER);
		this.mainFrame.add(this.buttonPanel, BorderLayout.SOUTH);

		this.mainFrame.setSize(400, 250);
		this.mainFrame.setVisible(true);
		this.mainFrame.setLocation(650, 400);

		this.ok.addActionListener(new ActionListenerOk());
		this.cancel.addActionListener(new ActionListenerCancel());

	}

	class ActionListenerText implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String empty = "";
			if ((word1 == null) || (word1.equals(empty)) || (word2 == null)
					|| (word2.equals(empty)) || (word3 == null)
					|| (word3.equals(empty)) || (word4 == null)
					|| (word4.equals(empty))) {
				ok.setEnabled(false);
			}
		}
	}

	class ActionListenerCancel implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			mainFrame.dispose();
		}
	}

	class ActionListenerOk implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ml.addWord(selectedUser + "," + word1.getText() + ","
					+ word2.getText() + "," + word3.getText() + ","
					+ word4.getText());
			mainFrame.dispose();
			System.out.println(selectedUser + "," + word1.getText() + ","
					+ word2.getText() + "," + word3.getText() + ","
					+ word4.getText());
		}
	}
}